package com.hgkj.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hgkj.common.core.domain.ApiResult;
import com.hgkj.common.core.domain.BaseController;
import com.hgkj.common.core.utils.AssertExceptionUtils;
import com.hgkj.system.domain.SysMenu;
import com.hgkj.system.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;


/**
 * 菜单管理
 *
 * @author hgkj
 */
@Validated
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 查询所有菜单
     *
     * @return 菜单list
     */
    @GetMapping
    @SaCheckPermission("system.menu.list")
    public ApiResult list() {
        return ApiResult.success(sysMenuService.listMenu());
    }

    /**
     * 新增菜单
     */
    @PostMapping
    @SaCheckPermission("system.menu.add")
    public ApiResult add(@Valid @RequestBody SysMenu sysMenu) {
        return getResult(sysMenuService.insertMenu(sysMenu));
    }

    /**
     * 修改菜单
     */
    @PutMapping
    @SaCheckPermission("system.menu.edit")
    public ApiResult edit(@RequestBody SysMenu sysMenu) {
        AssertExceptionUtils.notNull(sysMenu.getMenuId(), "menuId字段不能为空");
        return getResult(sysMenuService.editMenu(sysMenu));
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{menuId}")
    @SaCheckPermission("system.menu.remove")
    public ApiResult remove(@PathVariable @NotNull(message = "菜单ID不能为空")
                            @Min(value = 1, message = "菜单ID必须大于0") Long menuId) {
        return getResult(sysMenuService.deleteMenuById(menuId));
    }
}
